package org.jks.service;

import java.util.List;

import org.jks.model.Work;
import org.jks.model.WorkType;
import org.springframework.stereotype.Service;

@Service
public class WorkService extends EntityService<Work>{
	
	public WorkService(){
		MODEL_NAME = Work.class.getSimpleName();
		CLASS_NAME = Work.class;
	}
	
	public long countByCondition(WorkType workType, int tjType, 
			String startTime, String endTime, int userId){
		String hql = "select count(*) from " + MODEL_NAME + " where 1 = 1";
		if(workType != null){
			hql += " and Worktype.Id=" + workType.getId();
		}
		if(tjType != -1){
			
		}
		if(startTime != null && startTime.length() > 0){
			hql += " and Worktime >='" + startTime + " 00:00:00'";
		}
		if(endTime != null && endTime.length() > 0){
			hql += " and Worktime <='" + endTime + " 23:59:59'";
		}
		if(userId != -1){
			hql += " and Worker.Id=" + userId;
		}
		return super.count(hql);
	}
	
	public List<Work> search(WorkType workType, int tjType, 
			String startTime, String endTime, int userId, 
			int page, int pagesize){		
		String hql = "from " + MODEL_NAME + " where 1 = 1";
		if(workType != null){
			hql += " and Worktype.Id=" + workType.getId();
		}
		if(tjType != -1){
			
		}
		if(startTime != null && startTime.length() > 0){
			hql += " and Worktime >='" + startTime + " 00:00:00'";
		}
		if(endTime != null && endTime.length() > 0){
			hql += " and Worktime <='" + endTime + " 23:59:59'";
		}
		if(userId != -1){
			hql += " and Worker.Id=" + userId;
		}
		List<Object> params = null;
		return super.find(hql, params, page, pagesize);
	}
	
	public List<Work> list(){
		String hql = "from " + MODEL_NAME + " where EndTime is not null";
		return super.find(hql);
	}
	
	public List<Work> listWorkNotStart()
	{
		String hql = "from " + MODEL_NAME + " where State =0";
		return super.find(hql);
	}
}
